<?php
namespace app\api\controller\v1;
use think\Db;
use app\service\CouponService;
use think\Request;

class Mark extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		$this->IsLogin(); //验证登录
	}

	/**
	 * 获取优惠券列表
	 */
	public function index(Request $request)
	{
        $param = $request->param();
		$user = $this->user;
        if ($param['key'] != null){
            $info = Db::name('mark')->where('key',$param['key'])->find();
            if (!empty($info)){
                if ($info['is_use'] == 1){
                    $return['code'] = -1;
                    $return['msg'] = '兑换码已使用，请重试';
                    echo json_encode($return);
                    die;
                }
                $update = ['user_id'=> $user['id'],'upd_time'=>time(),'is_use'=>1];
                $re = Db::name('mark')->where('id',$info['id'])->update($update);
                if ($re){
                    $userm = Db::name('user')->where('id',$user['id'])->value('money');
                    if(!empty($userm)){
                        $userm += $info['money'];
                        $ree = Db::name('user')->where('id',$user['id'])->update(['money'=>$userm]);
                        Db::name('mark_log')->insert(['mark_id'=>$info['id'],'user_id'=>$user['id'],'add_time'=>time(),'user_money'=>$userm-$info['money'],'mmoney'=>$info['money'],'lastmoney'=>$userm]);
                        Db::name('pay_log')->insert(['order_id'=>$info['id'],'pay_price'=>$info['money'],'add_time'=>time(),'total_price'=>$info['money'],'subject'=>'兑换码充值','payment'=>'Weixin','payment_name'=>'充值码','business_type'=>1]);
                    }
                }
            }else{
                $return['code'] = -1;
                $return['msg'] = '兑换码错误，请重试';
                echo json_encode($return);
                die;
            }
        }
        if ($ree){
            $return['code'] = 0;
            $return['msg'] = '成功充值'.$info['money'].'元';
        }else{
            $return['code'] = -1;
            $return['msg'] = '充值失败，请重试';
        }

        echo json_encode($return);

	}

    public function lahei(Request $request){
        $param = $request->param();
        $order_info = Db::name('order')->where('id',$param['order_id'])->find();
        $user = $this->user;

        $not_show = Db::name('user')->where('id',$user['id'])->value('not_show');
        if (!empty($not_show)){
            $not_list = explode(',',$not_show);
            if (in_array($order_info['techn_id'],$not_list)){
                echo json_encode(['code'=>0,'msg'=>'拉黑成功']);
                exit();
            }else{
                array_push($not_list,$order_info['techn_id']);
                $up = implode(',',$not_list);
            }
        }else{
            $up = $order_info['techn_id'];
        }
        $result = Db::name('user')->where('id',$user['id'])->update(['not_show'=>$up]);
        if ($result){
            echo json_encode(['code'=>0,'msg'=>'拉黑成功']);
            exit();
        }else{
            echo json_encode(['code'=>-1,'msg'=>'拉黑失败,请重试']);
            exit();
        }
    }
}